import { useLang } from "@/hooks/useLang" // 拿到语言

const { ActiveLangName } = useLang()

/** 数组去重 */ 
export function removeDuplicate(arr:any[],key:string) {
  const newArr:any[] = []
  arr.forEach(item => {
    if (!newArr.includes(item[key])) {
      newArr.push(item)
    }
  })
  return newArr
}

/**
 * 小写金额 转大写金额
 * */ 
  export function  dealBigMoney(n:any):string{       
    if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
    return "数据非法";
    let unit = "千百拾亿千百拾万千百拾元角分", str = "";
    n += "00";
    let p = n.indexOf('.');
    if (p >= 0){
        n = n.substring(0, p) + n.substr(p+1, 2);
    unit = unit.substr(unit.length - n.length);
  }
    
    for (var i = 0; i < n.length; i++)
      str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
      return  str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
  }

/**
 * 加减乘除计算
 * arg1 : number| string 
 * arg2 : number| string  
 * */ 

  // 加
  export const accAdd = (arg1:number,arg2:number):number =>{
    var r1, r2, m;
    try {
      r1 = arg1.toString().split('.')[1].length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2.toString().split('.')[1].length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));

	  return (arg1 * m + arg2 * m) / m;

  }
  // 减
  export const subFloatToInt = (arg1:number,arg2:number): number|string=>{
    var r1, r2, m, n;
    try {
      r1 = arg1.toString().split('.')[1].length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2.toString().split('.')[1].length;
    } catch (e) {
      r2 = 0;
    }
      m = Math.pow(10, Math.max(r1, r2));
    //动态控制精度长度
      n = (r1 >= r2) ? r1 : r2;
	  return (( arg1 * m - arg2 * m) / m).toFixed(n);
  }
  // 乘
  export const accMul = (arg1:number,arg2:number):number =>{
    var m = 0,
      s1 = arg1.toString(),
      s2 = arg2.toString();
    try {
      m += s1.split('.')[1].length;
    } catch (e) {}
    try {
      m += s2.split('.')[1].length;
    } catch (e) {}
    return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
  }
  // 除
  export const accDiv = (arg1:number,arg2:number): number| string=>{
    var t1 = 0, t2 = 0, r1, r2;
    try {
      t1 = arg1.toString().split('.')[1].length;
    } catch (e) {}
    try {
      t2 = arg2.toString().split('.')[1].length;
    } catch (e) {}
    r1 = Number(arg1.toString().replace('.', ''));
    r2 = Number(arg2.toString().replace('.', ''));
    return (r1 / r2) * Math.pow(10, t2 - t1);
  }

// 所有空格
const allTrim = (str:string):string=>{
  return str.replace(/^(\s*)|(\s*$)/g,"")
}

// 去除 左空格
const leftTrim = (str:string):string =>{
  return str.replace(/^(\s*)/g,"")
}

// 去除 右空格
const rightTrim = (str:string):string =>{
  return str.replace(/(\s*$)/g,"")
}



// 去除空格
/**
 * type: 1: 去除左右空格, 2: 去除左空格,  3: 去除又空格 
 * */ 
export const trim = (data:any[] | string ,type:(1|2|3|''))=>{
  type = type || 1
  if(Array.isArray(data)){
    data = data.map(v=>{
      if(typeof v == "string"){
        v = type == 1 ? allTrim(v) : type==2 ? leftTrim(v) : rightTrim(v)
      }else {
        for(let key in v){
          v[key] = type == 1 ? allTrim(v[key]) : type==2 ? leftTrim(v[key]) : rightTrim(v[key])
        }
      }
      return v
    })
  }
  if(typeof data === "string"){
    data = type == 1 ? allTrim(data) : type==2 ? leftTrim(data) : rightTrim(data)
    
  }

  return data

}


// 浏览器添加朗读功能
export const speechSynthesisUtter = (textContent:string)=>{
  if(!textContent.trim()){
    return false
  }
  if('speechSynthesis' in window){
    const utterance = new SpeechSynthesisUtterance()
    utterance.text = textContent // 要朗读的文本内容
    utterance.lang = ActiveLangName.value // 设置语言
    utterance.rate = 1.2 // 语速，默认为1.0
    utterance.pitch = 1.5 // 音高，默认为1.0
    speechSynthesis.speak(utterance)
  }
}






/** 将全局 CSS 变量导入 JS 中使用 */
  export const getCssVariableValue = (cssVariableName: string) => {
      let cssVariableValue = ""
      try {
        // 没有拿到值时，会返回空串
        cssVariableValue = getComputedStyle(document.documentElement).getPropertyValue(cssVariableName)
      } catch (error) {
        console.error(error)
      }
      return cssVariableValue
  }